package com.keldian.zhbj74.activity;

import java.util.ArrayList;
import java.util.List;

import com.keldian.zhbj74.R;
import com.keldian.zhbj74.utils.DensityUtils;
import com.keldian.zhbj74.utils.SharePrefencesUtils;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class GuideActivity extends Activity {
	private ViewPager mViewPager;

	private int[] mImgs = new int[] { R.drawable.guide_1, R.drawable.guide_2,
			R.drawable.guide_3 };

	private List<ImageView> mListImgs;
	private LinearLayout mLlContainerLayout;
	private ImageView ivRedPoint;// 小红点1

	private int mPointDis;
	private Button btnStart;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_guide);
		mLlContainerLayout = (LinearLayout) findViewById(R.id.ll_container);
		mViewPager = (ViewPager) findViewById(R.id.vp_guide);
		ivRedPoint = (ImageView) findViewById(R.id.iv_red_point);
		btnStart = (Button) findViewById(R.id.btn_start);

		initData();
		mViewPager.setAdapter(new GudieAdapter());
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == mListImgs.size() - 1) {
					btnStart.setVisibility(View.VISIBLE);
				} else {
					btnStart.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				int leftMargin = (int) (mPointDis * arg1) + arg0 * mPointDis;
				RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) ivRedPoint
						.getLayoutParams();
				layoutParams.leftMargin = leftMargin;
				ivRedPoint.setLayoutParams(layoutParams);

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		ivRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						ivRedPoint.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						mPointDis = mLlContainerLayout.getChildAt(1).getLeft()
								- mLlContainerLayout.getChildAt(0).getLeft();

					}
				});

		btnStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharePrefencesUtils.setBoolean(getApplicationContext(),
						"is_first_enter", false);

				// 跳到主页面
				startActivity(new Intent(getApplicationContext(),
						MainActivity.class));
				finish();
			}
		});

	}

	private void initData() {
		mListImgs = new ArrayList<ImageView>();
		for (int i = 0; i < mImgs.length; i++) {
			ImageView view = new ImageView(this);
			view.setBackgroundResource(mImgs[i]);
			mListImgs.add(view);

			ImageView point = new ImageView(this);

			point.setImageResource(R.drawable.shape_point_gray);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);

			if (i > 0) {
				params.leftMargin = DensityUtils.dip2px(10, this);
			}

			point.setLayoutParams(params);
			mLlContainerLayout.addView(point);

		}
	}

	class GudieAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mListImgs.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListImgs.get(position));
			return mListImgs.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}
}
